<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class SentSpamPeer extends BaseSentSpamPeer
{
	const PROF_JOIN_TAG = "prof_join";
	const COURSE_ADMIN_JOIN_TAG = "ca_join";
	
	
	public static function logProfessorJoinSpam($user_id){
		self::logJoinSpam($user_id, "prof");
	}
	
	public static function logCourseAdminJoinSpam($user_id){
		self::logJoinSpam($user_id, "ca");
	}
	
	public static function logJoinSpam($user_id, $tag){
		$s = new SentSpam();
		$s->setUserId($user_id);
		$s->setTag($tag);
		$s->save();
	}
	
	public static function beenSpammed($user_id){
		$c = new Criteria();
		$c->add(SentSpamPeer::USER_ID, $user_id);
	    $spam = self::doSelectOne($c);
	    if(!$spam || $spam == null)
	    	return false;
	    return true;
	}
	
	public static function beenSpammedByEmail($email){
		$user = UserPeer::retrieveByEmail($email);
		if(!$user)
			return false;
		return self::beenSpammed($user->getId());
	}
	
	public static function getProfInvite($email, $course_name){
		$user = new User();
		$user->setEmail($email);
		
		//create a username
		$user->setUsername($email);
		//$user->setNetworkId($prof->getUser()->getNetworkId());
		//$user->setNetworkName($prof->getUser()->getNetworkName());
		$initial_password = UserPeer::createRandomPassword();
	    $user->setMd5Password(md5($initial_password));
	    $user->setLanguage('en');
	    $user->setActivated(0);	// this account is new and needs to be activated
	    $user->save();

	    $mailContext = array('name' => $user->getEmail(),
	    							'init_pass' => $initial_password ,
	    							'course_name'=> $course_name);
	    
	    $template = "_welcomeToFinalsClub";
	    
	    return self::get_component("sentSpam","sendInvite",array("user" => $user, 
	    														"template" => $template, 
	    														"mailContext" => $mailContext, 
	    														"tag" => SentSpamPeer::PROF_JOIN_TAG));
	    
	}
	
	public static function getCourseAdminInvite($email, $course_name, Prof $prof){
			$user = new User();
			$user->setEmail($email);
			
			//create a username
			$user->setUsername($email);
			$user->setNetworkId($prof->getUser()->getNetworkId());
			$user->setNetworkName($prof->getUser()->getNetworkName());
			$initial_password = UserPeer::createRandomPassword();
		    $user->setMd5Password(md5($initial_password));
		    $user->setLanguage('en');
		    $user->setActivated(0);	// this account is new and needs to be activated
		    $user->save();

		    $mailContext = array('name' => $user->getEmail(),
		    							'init_pass' => $initial_password ,
		    							'course_name'=> $course_name, 
		    							'prof_name' => $prof->getUser()->getFullName());
		    
		    $template = "_courseAdminWelcome";
		    echo self::get_component("sentSpam","sendInvite",array("user" => $user, 
										    						"template" => $template, 
										    						"mailContext" => $mailContext, 
										    						"tag" => SentSpamPeer::COURSE_ADMIN_JOIN_TAG));
		    return $user;
		    
		    		
	}
/**
 * Statically access a component
 * 
 * @param $moduleName
 * @param $componentName
 * @param $vars
 * @return unknown_type
 */	
public static function get_component($moduleName, $componentName, $vars = array()){
	

	$context = sfContext::getInstance();
	$context_action = $context->getController()->getAction('notes', 'index');  // gets the controller calling the method
	return $context_action->getComponent("sentSpam","sendInvite",$vars);
	
	
	}


public static function get_partial($partialName,$vars){
	$context = sfContext::getInstance();
	$context_action = $context->getController()->getAction('sentSpam','index');
	return $context_action->getPartial($partialName,$vars);
	}

}